package com.nosweattechnet.placemap;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import com.nosweattechnet.datatypes.Item;
import com.nosweattechnet.server.ItemIDAsyncTask;

//This class is in charge of displaying items on a place map
public class DisplayItems {
	private static ArrayList<Item> items=new ArrayList<Item>();
	private static String userid="";
	private static String placeid="";
	
	public static void setUserID(String user_id){
		userid=user_id;
	}
	public static void setPlaceID(String place_id){
		placeid=place_id;
	}
	public static String getPlaceID(){
		return placeid;
	}
	//Checks to see if item is valid and then adds it to the
	//ArrayList of items to be displayed on the map
	public static void addItem(Item item){
		if(shouldAdd(item)){
			items.add(item);
		}
	}
	//Checks validity of multiple items and then adds them
	//to ArrayList of items to be displayed on the map
	public static void addItems(ArrayList<Item> newitems){
		for(Item i:newitems){
			if(shouldAdd(i)){
				items.add(i);
			}
		}
	}
	
	public static void clear(){
		items.clear();
	}
	
	public static int size(){
		return items.size();
	}
	
	public static float getXPos(int i){
		return items.get(i).getXPos();
	}
	
	public static float getYPos(int i){
		return items.get(i).getYPos();
	}
	
	public static float getZPos(int i){
		return items.get(i).getZPos();
	}
	
	public static ItemMarker getItemMarker(PlaceMapActivity c, int i){
		return new ItemMarker(c,items.get(i),userid);
	}
	
	//Reloads an item given its ID
	public static void reloadItem(String itemid){
		for(int i=0;i<items.size();++i){
			if(items.get(i).getId().equals(itemid)){
				ItemIDAsyncTask iiat=new ItemIDAsyncTask(itemid);
				iiat.execute();
				try {
					items.add(iiat.getItem());
					items.remove(i);
				} catch (InterruptedException e) {
				} catch (ExecutionException e) {
				}
				return;
			}
		}
	}
	
	//Filters out duplicate items
	private static boolean shouldAdd(Item item){
		if(item==null){
			return false;
		}
		for(Item i:items){
			if(i.equals(items)){
				return false;
			}
		}
		return true;
	}
	
}
